﻿Imports System.Text.RegularExpressions
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class InscriptionClients
    Dim messageerreur As String
    Dim noEntreprise As Integer
    Dim codeProvince As String
    Dim dbClients As New cls_taco
    Dim obligationChamp As Boolean
    Dim noClient As Integer

    Private Sub InscriptionClients_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        frmGestionClients.btnRechercher.PerformClick()
        Me.Dispose()
    End Sub

    Public Overloads Function ShowDialog(ByVal idDuClient As Integer, Optional ByVal saisieObligatoire As Boolean = False) As Windows.Forms.DialogResult
        obligationChamp = saisieObligatoire
        If obligationChamp = True Then
            lblNoAss.Visible = True
            lblNoPermis.Visible = True

        End If


        If idDuClient <> Nothing Then
            remplirInfo(idDuClient)
            btnAjouter.Text = "Enregistrer"
            reglerPanelEntreprise()
            noClient = idDuClient
        Else
            remplirInfoParDefaut()
            btnAjouter.Text = "Ajouter"
            panEntreprise.Enabled = False
        End If


        Return MyBase.ShowDialog()
    End Function


    Private Sub InscriptionClients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtNaiss.MaxDate = Now

        'reglerEtat()

    End Sub

    Public Sub remplirInfoParDefaut()



        remplirListCbo("province", "Choisissez une province", cboProvince, "")

        remplirListCbo("ville", "Choisissez une province", cboVille, " WHERE v.CodeProvince = p.CodeProvince AND p.nomProvince = '" & cboProvince.SelectedItem & "'")
        cboTypeClient.Text = cboTypeClient.Items(0)

    End Sub

    Public Sub remplirInfo(ByVal v_noClient)
        'créé la connection à la base de données
        dbClients = New cls_taco



        dbClients.defDataset("SELECT  commentaire, codePostal as CodePostal, CodeProvince as CodeProvince, noPermisConduire as noPermisConduire, nom AS Nom, prenom AS Prenom, dateNaissance as DateDeNaissance, adresse as Adresse, " & _
        "ville as Ville, telephone as Telephone, accidentDerniereAnnee as Accident, responsable as Responsable, typeClient as TypeClient, noEntreprise as noEntreprise,personneContact as Personne, postePersonneContact as Poste, courriel as Courriel, NAS as NAS, cellulaire as Cell, actif as Actif, noClient " _
       & "FROM tblClients where noClient = " & v_noClient & ";", "tblClients")




        'vide la listView


        'créé une colonne dans la listView pour chaque élément de la requête SQL
        For Each colonne As DataColumn In dbClients.ds.Tables("tblClients").Columns
            'For i = 0 To dsClients.Tables("tblClients").Columns.Count
            '    MsgBox(dsClients.Tables("tblClients").Rows(0).Item(i))
            'Next
            With dbClients.ds.Tables("tblClients")
                Select Case colonne.ColumnName
                    Case "noPermisConduire"
                        txtNoPermisConduire.Text = nvl(.Rows(0).Item("noPermisConduire"), "")
                    Case "Nom"
                        txtNom.Text = nvl(.Rows(0).Item("Nom"), "")
                    Case "Cell"
                        txtCell.Text = nvl(.Rows(0).Item("Cell"), "")
                    Case "Prenom"
                        txtPrenom.Text = nvl(.Rows(0).Item("Prenom"), "")
                    Case "DateDeNaissance"
                        dtNaiss.Value = nvl(.Rows(0).Item("DateDeNaissance"), "")
                        'dtNaiss.Value = CDate("2-2-2009")
                        txtAge.Text = calcul_age(dtNaiss.Value)
                    Case "Adresse"
                        txtAdresse.Text = nvl(.Rows(0).Item("Adresse"), "")
                    Case "commentaire"
                        txtCommentaire.Text = nvl(.Rows(0).Item("commentaire"), "")
                    Case "CodePostal"
                        txtCodePostal.Text = nvl(.Rows(0).Item("codePostal"), "")
                    Case "CodeProvince"
                        ' MsgBox(dsClients.Tables("tblClients").Rows(0).Item("Province"))

                        codeProvince = nvl(.Rows(0).Item("CodeProvince"), "")

                        remplirListCbo("province", cboProvince.Text, cboProvince, "")
                        cboProvince.Text = getProvince(codeProvince)
                    Case "Ville"
                        cboVille.Text = nvl(.Rows(0).Item("Ville"), "")
                        remplirListCbo("ville", cboVille.Text, cboVille, " WHERE v.CodeProvince = p.CodeProvince AND p.nomProvince = '" & cboProvince.SelectedItem & "'")
                    Case "Courriel"
                        txtCourriel.Text = nvl(.Rows(0).Item("Courriel"), "")
                    Case "Telephone"
                        txtTel.Text = nvl(.Rows(0).Item("Telephone"), "")
                    Case "NAS"
                        txtNas.Text = nvl(.Rows(0).Item("NAS"), "")
                    Case "Accident"

                        If nvl(.Rows(0).Item("Accident"), "false") = "True" Then
                            optOuiAccident.Checked = True
                        Else
                            optNonAccident.Checked = True
                            panResponsable.Enabled = False
                        End If
                    Case "Responsable"

                        If nvl(.Rows(0).Item("Responsable"), "false") = "True" Then
                            optOuiResponsable.Checked = True
                        Else
                            optNonResponsable.Checked = True
                        End If
                    Case "Actif"

                        If .Rows(0).Item("Actif") = "True" Then
                            chkActif.Checked = True
                        Else
                            chkActif.Checked = False
                        End If
                    Case "TypeClient"
                        'le client doit avoir un type, sinon ça bug
                        cboTypeClient.Text = nvl(.Rows(0).Item("TypeClient"), "")


                    Case "noEntreprise"

                        Dim dbEntreprise = New cls_taco
                        noEntreprise = nvl(.Rows(0).Item("noEntreprise"), 0)
                        updateEntreprise(noEntreprise)

                    Case "Personne"
                        TxtContact.Text = nvl(.Rows(0).Item("Personne"), "")
                    Case "Poste"
                        txtPoste.Text = nvl(.Rows(0).Item("Poste"), "")
                End Select
            End With
        Next

        'créé un élément dans le listView pour chaque enregistrement retourné par la requête SQL

    End Sub

    Public Sub updateEntreprise(ByVal idenEntreprise As Integer)
        noEntreprise = idenEntreprise
        txtNomEntreprise.Text = getEntreprise(idenEntreprise, "nom")
        txtAdresseEntreprise.Text = getEntreprise(idenEntreprise, "adresse")
    End Sub




    Private Sub btnAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        Me.Close()

    End Sub



    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub cboTypeClient_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTypeClient.SelectedIndexChanged
        reglerPanelEntreprise()
        If cboTypeClient.Text = "Personnel" Then
            TxtContact.Text = Nothing
            txtPoste.Text = Nothing
            txtNomEntreprise.Text = Nothing
            txtAdresseEntreprise.Text = Nothing
        End If
    End Sub

    Public Sub reglerPanelEntreprise()
        If cboTypeClient.Text = "Entreprise" Then
            panEntreprise.Enabled = True
        Else
            panEntreprise.Enabled = False
        End If
    End Sub

    Public Function validerDonnee()
        Return validerTel(txtTel.Text, messageerreur, False) = True And validerNom(txtNom.Text, messageerreur, False) _
        And validerNom(txtPrenom.Text, messageerreur, False) And validerAdresse(txtAdresse.Text, messageerreur, False) _
        And validerNas(txtNas.Text, messageerreur, Not obligationChamp) And validerNoPermis(txtNoPermisConduire.Text, messageerreur, Not obligationChamp) _
       And validerVille(cboVille.Text, messageerreur, False) And validerTel(txtCell.Text, messageerreur, True) _
       And validerEmail(txtCourriel.Text, messageerreur, True) And validerCodePostal(txtCodePostal.Text, messageerreur, False) _
       And (IIf(panEntreprise.Enabled = True, validerNom(TxtContact.Text, messageerreur, False), True))


    End Function

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        txtAdresse.Text = Trim(txtAdresse.Text)
        txtNom.Text = Trim(txtNom.Text)
        txtPrenom.Text = Trim(txtPrenom.Text)
        txtTel.Text = Trim(txtTel.Text)
        txtCodePostal.Text = Trim(txtCodePostal.Text)
        txtNoPermisConduire.Text = Trim(txtNoPermisConduire.Text)
        txtNas.Text = Trim(txtNas.Text)
        txtCell.Text = Trim(txtCell.Text)
        txtCourriel.Text = Trim(txtCourriel.Text)
        cboVille.Text = Trim(cboVille.Text)
        txtCommentaire.Text = Trim(txtCommentaire.Text)
        TxtContact.Text = Trim(TxtContact.Text)
        txtPoste.Text = Trim(txtPoste.Text)
        txtNom.Text = StrConv(txtNom.Text, VbStrConv.ProperCase)
        txtPrenom.Text = StrConv(txtPrenom.Text, VbStrConv.ProperCase)
        cboVille.Text = StrConv(cboVille.Text, VbStrConv.ProperCase)
        messageerreur = "Erreur: "
        If validerDonnee() Then
            If villeExiste(cboVille.Text) = False Then
                ajouterVilles(cboVille.Text, getCodeProv(cboProvince.Text))
            End If

            If btnAjouter.Text = "Enregistrer" Then
                For Each client As DataRow In dbClients.ds.Tables("tblClients").Rows

                    client.Item("CodePostal") = txtCodePostal.Text
                    client.Item("CodeProvince") = getCodeProv(cboProvince.Text)
                    client.Item("noPermisConduire") = txtNoPermisConduire.Text
                    client.Item("NAS") = txtNas.Text
                    client.Item("Nom") = txtNom.Text
                    client.Item("Cell") = txtCell.Text
                    client.Item("Prenom") = txtPrenom.Text
                    client.Item("Courriel") = txtCourriel.Text
                    client.Item("DateDeNaissance") = dtNaiss.Value
                    client.Item("Adresse") = txtAdresse.Text
                    client.Item("Ville") = cboVille.Text
                    client.Item("Telephone") = txtTel.Text
                    client.Item("Actif") = chkActif.Checked
                    client.Item("commentaire") = txtCommentaire.Text
                    client.Item("TypeClient") = cboTypeClient.Text
                    client.Item("Personne") = TxtContact.Text
                    client.Item("Poste") = txtPoste.Text
                    If cboTypeClient.Text = "Entreprise" Then

                        If txtNomEntreprise.Text = "" Then
                            MsgBox("Vous devez sélectionner une entreprise.")
                            Exit Sub
                        End If

                        'client.Item("noEntreprise") = getNoEntreprise(txtNomEntreprise.Text)
                        client.Item("noEntreprise") = getNoEntreprise(txtNomEntreprise.Text, txtAdresseEntreprise.Text)
                    Else
                        client.Item("noEntreprise") = DBNull.Value
                    End If

                    If optOuiAccident.Checked = True Then
                        client.Item("Accident") = "true"
                    Else
                        client.Item("Accident") = "false"
                    End If

                    If optOuiResponsable.Checked = True Then
                        client.Item("Responsable") = "true"
                    Else
                        client.Item("Responsable") = "false"
                    End If


                Next

                dbClients.update("tblClients")

            ElseIf btnAjouter.Text = "Ajouter" Then


                ajouterClients(txtCodePostal.Text, getCodeProv(cboProvince.Text), txtNoPermisConduire.Text, txtNas.Text, txtCell.Text, txtNom.Text, txtPrenom.Text, txtCourriel.Text, dtNaiss.Value, txtAdresse.Text, cboVille.Text, txtTel.Text, chkActif.Checked, cboTypeClient.Text, TxtContact.Text, txtPoste.Text, getNoEntreprise(txtNomEntreprise.Text, txtAdresseEntreprise.Text), optOuiAccident.Checked, optOuiResponsable.Checked, txtCommentaire.Text)


            End If


            Me.Close()
        Else
            MsgBox(messageerreur)
        End If
    End Sub

    

    Private Sub cboProvince_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboProvince.SelectionChangeCommitted
        remplirListCbo("ville", "Choisissez une ville", cboVille, " WHERE v.CodeProvince = p.CodeProvince AND p.nomProvince = '" & cboProvince.SelectedItem & "'")
    End Sub

    Private Sub txtCodePostal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodePostal.TextChanged

    End Sub

    Private Sub optNonAccident_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optNonAccident.CheckedChanged
        If optNonAccident.Checked = True Then
            optNonResponsable.PerformClick()
            panResponsable.Enabled = False


        End If
    End Sub

    Private Sub optOuiAccident_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optOuiAccident.CheckedChanged
        If optOuiAccident.Checked = True Then
            optNonResponsable.PerformClick()
            panResponsable.Enabled = True
        End If
    End Sub

    Private Sub cboProvince_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboProvince.SelectedIndexChanged

    End Sub

    Private Sub cboVille_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboVille.SelectedIndexChanged

    End Sub

    Private Sub dtNaiss_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtNaiss.ValueChanged
        txtAge.Text = calcul_age(dtNaiss.Value)
    End Sub

    Private Sub btnRechercher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercher.Click
        frmGestionEntreprises.ShowDialog(True)

    End Sub

   
    Private Sub txtAge_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAge.TextChanged

    End Sub

    Private Sub Label22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label22.Click

    End Sub

    Private Sub Label23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label23.Click

    End Sub

    Private Sub Label30_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label30.Click

    End Sub

    Private Sub txtPoste_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoste.KeyPress
        If Not IsNumeric(e.KeyChar) And e.KeyChar <> ControlChars.Back And e.KeyChar <> ControlChars.Tab Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPoste_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPoste.TextChanged

    End Sub
End Class